package ru.pb.game.data.xml.parsers;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import ru.pb.game.data.templates.players.PlayerTemplate;
import ru.pb.game.data.xml.holders.PlayerTemplateHolder;
import ru.pb.global.utils.ParserUtils;
import ru.pb.global.utils.StatsSet;
import ru.pb.global.xml.parsers.AbstractFileParser;

/**
 * @author: Felixx
 */
public final class PlayerTemplateParser extends AbstractFileParser<PlayerTemplateHolder>
{
	private static PlayerTemplateParser _instance;

	public static PlayerTemplateParser getInstance()
	{
		if(_instance == null)
			_instance = new PlayerTemplateParser();
		return _instance;
	}

	private PlayerTemplateParser()
	{
		super("./data/templates/playerTemplate.xml", PlayerTemplateHolder.getInstance());
	}

	@Override
	protected void readData(Node start0, String fileName)
	{
		if("list".equalsIgnoreCase(start0.getNodeName()))
		{
			for(Node n = start0.getFirstChild(); n != null; n = n.getNextSibling())
			{
				StatsSet set = new StatsSet();
				if(n.getNodeName().equalsIgnoreCase("template"))
				{
					NamedNodeMap attr = n.getAttributes();
					set.set("id", ParserUtils.intValue(attr.getNamedItem("id")));

					for(Node s = n.getFirstChild(); s != null; s = s.getNextSibling())
					{
						if("set".equalsIgnoreCase(s.getNodeName()))
						{
							NamedNodeMap statsMap = s.getAttributes();
							set.set(statsMap.getNamedItem("name").getNodeValue(), statsMap.getNamedItem("val").getNodeValue());
						}
					}
					getHolder().addTemplate(new PlayerTemplate(set));
				}
			}
		}
	}
}